package com.youwatch.concurrency.example.singleton;

import com.youwatch.concurrency.annotations.Recommend;
import com.youwatch.concurrency.annotations.ThreadSafe;

/**
 * @Author: kss
 * @Description: 枚举模式：最安全
 * @Date: Create in 2019/1/17
 */
@ThreadSafe
@Recommend
public class SingletonEnumSafeRecommend {

    private SingletonEnumSafeRecommend() {}

    public static SingletonEnumSafeRecommend getInstance() {
        return Singleton.INSTANCE.getInstance();
    }

    private enum Singleton {
        INSTANCE;

        private SingletonEnumSafeRecommend singleton;

        // JVM保证这个方法绝对只调用一次
        Singleton() {
            singleton = new SingletonEnumSafeRecommend();
        }

        public SingletonEnumSafeRecommend getInstance() {
            return singleton;
        }
    }
}
